package and.example.addresscontacts;

import and.example.data.DatabaseConnector;
import and.example.interfaces.IArguments;
import android.app.Activity;
import android.app.AlertDialog.Builder;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class ViewContact extends Activity implements IArguments {

	long rowID;
	TextView name;
	TextView phone;
	TextView email;
	TextView street;
	TextView city;

	ImageView iv;
	private String imagePath = "";

	ProgressDialog pd;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.view_contact);

		pd = ProgressDialog.show(this, "", "loading...");

		name = (TextView) findViewById(R.id.nameTextView);
		phone = (TextView) findViewById(R.id.phoneTextView);
		email = (TextView) findViewById(R.id.emailTextView);
		street = (TextView) findViewById(R.id.streetTextView);
		city = (TextView) findViewById(R.id.cityTextView);

		iv = (ImageView) findViewById(R.id.imageview);

		rowID = getIntent().getLongExtra(MainActivity.ROW_ID, 1);

		RelativeLayout layout = (RelativeLayout) findViewById(R.id.header_layout);
		ImageButton btn = (ImageButton) layout.findViewById(R.id.header_left_btn);
		btn.setOnClickListener(new android.view.View.OnClickListener() {
			@Override
			public void onClick(View v) {
				ViewContact.this.finish();
			}
		});
		
		Button buttonMenu = (Button)findViewById(R.id.header_right_btn);
		registerForContextMenu(buttonMenu);
		buttonMenu.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				openContextMenu(v);
			}
		});
	}

	@Override
	protected void onResume() {
		super.onResume();
		new LoadContactTask().execute(rowID);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.view_contact_menu, menu);
		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {

		switch (item.getItemId()) {
		case R.id.editItem:
			Intent i = new Intent(this, AddEditContact.class);

			i.putExtra(MainActivity.ROW_ID, rowID);
			i.putExtra(COLUMNS[2], name.getText().toString());
			i.putExtra(COLUMNS[3], phone.getText().toString());
			i.putExtra(COLUMNS[4], email.getText().toString());
			i.putExtra(COLUMNS[5], street.getText().toString());
			i.putExtra(COLUMNS[6], city.getText().toString());
			i.putExtra(COLUMNS[7], imagePath);

			startActivity(i);
			this.finish();
			break;

		case R.id.sendEmail:
			Intent emailIntent = new Intent(this, SendEmailActivity.class);
			emailIntent.putExtra(COLUMNS[4], email.getText().toString());
			startActivity(emailIntent);
			this.finish();
			break;

		case R.id.callPhone:
			Intent call = new Intent(Intent.ACTION_CALL);
			call.setData(Uri.parse("tel:" + phone.getText().toString()));
			startActivity(call);
			this.finish();
			break;

		case R.id.sendSMS:
			Intent smsIntend = new Intent(this, SendSMSActivity.class);
			smsIntend.putExtra(COLUMNS[3], phone.getText().toString());
			startActivity(smsIntend);
			this.finish();
			break;

		case R.id.deleteItem:
			deleteContact();
			break;

		default:
			break;
		}

		return super.onOptionsItemSelected(item);
	}
	
	@Override
	public void onCreateContextMenu(ContextMenu menu, View v,
			ContextMenuInfo menuInfo) {
		getMenuInflater().inflate(R.menu.view_contact_menu, menu);
		super.onCreateContextMenu(menu, v, menuInfo);
	}
	
	@Override
	public boolean onContextItemSelected(MenuItem item) {
		
		switch (item.getItemId()) {
		case R.id.editItem:
			Intent i = new Intent(this, AddEditContact.class);

			i.putExtra(MainActivity.ROW_ID, rowID);
			i.putExtra(COLUMNS[2], name.getText().toString());
			i.putExtra(COLUMNS[3], phone.getText().toString());
			i.putExtra(COLUMNS[4], email.getText().toString());
			i.putExtra(COLUMNS[5], street.getText().toString());
			i.putExtra(COLUMNS[6], city.getText().toString());
			i.putExtra(COLUMNS[7], imagePath);

			startActivity(i);
			this.finish();
			break;

		case R.id.sendEmail:
			Intent emailIntent = new Intent(this, SendEmailActivity.class);
			emailIntent.putExtra(COLUMNS[4], email.getText().toString());
			startActivity(emailIntent);
			this.finish();
			break;

		case R.id.callPhone:
			Intent call = new Intent(Intent.ACTION_CALL);
			call.setData(Uri.parse("tel:" + phone.getText().toString()));
			startActivity(call);
			this.finish();
			break;

		case R.id.sendSMS:
			Intent smsIntend = new Intent(this, SendSMSActivity.class);
			smsIntend.putExtra(COLUMNS[3], phone.getText().toString());
			startActivity(smsIntend);
			this.finish();
			break;

		case R.id.deleteItem:
			deleteContact();
			break;

		default:
			break;
		}

		return super.onContextItemSelected(item);
	}

	private void deleteContact() {

		Builder builder = new Builder(this);
		builder.setTitle(R.string.confirmTitle)
				.setMessage(R.string.confirmMessage)
				.setPositiveButton(R.string.button_delete,
						new OnClickListener() {
							@Override
							public void onClick(DialogInterface dialog,
									int which) {
								delete();
							}
						}).setNegativeButton(R.string.button_cancel, null)
				.show();

	}

	private void delete() {

		final DatabaseConnector databaseConnector = new DatabaseConnector(
				ViewContact.this);

		AsyncTask<Long, Object, Object> deleteTask = new AsyncTask<Long, Object, Object>() {

			@Override
			protected Object doInBackground(Long... params) {

				databaseConnector.deleteContact(params[0]);
				return null;
			}

			@Override
			protected void onPostExecute(Object result) {
				super.onPostExecute(result);
				finish();
			}
		};

		deleteTask.execute(new Long[] { rowID });
	}

	private class LoadContactTask extends AsyncTask<Long, Object, Cursor> {

		DatabaseConnector databaseConnector = new DatabaseConnector(
				ViewContact.this);

		@Override
		protected Cursor doInBackground(Long... params) {
			databaseConnector.open();
			return databaseConnector.getOneContact(params[0]);
		}

		@Override
		protected void onPostExecute(Cursor result) {
			super.onPostExecute(result);

			result.moveToFirst();
			int name1 = result.getColumnIndex(COLUMNS[2]);
			int phone1 = result.getColumnIndex(COLUMNS[3]);
			int email1 = result.getColumnIndex(COLUMNS[4]);
			int street1 = result.getColumnIndex(COLUMNS[5]);
			int city1 = result.getColumnIndex(COLUMNS[6]);
			int image1 = result.getColumnIndex(COLUMNS[7]);

			name.setText(result.getString(name1));
			phone.setText(result.getString(phone1));
			email.setText(result.getString(email1));
			street.setText(result.getString(street1));
			city.setText(result.getString(city1));

			imagePath = result.getString(image1);

			Bitmap bmp = BitmapFactory.decodeFile(imagePath);
			if (bmp != null) {
				iv.setImageBitmap(bmp);
			} else {
				iv.setImageResource(R.drawable.contact2);
			}

			result.close();
			databaseConnector.close();
			pd.dismiss();
		}

	}
}
